import { defineConfig, UserConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// @ts-ignore
import fs from 'fs'
// @ts-ignore
import dotenv from 'dotenv'
/* 路径配置 start */
import { resolve } from 'path'
import postCssPxToRem from 'postcss-pxtorem'

// https://vitejs.dev/config/
export default defineConfig(({ mode } : UserConfig): UserConfig => {
  // 根据环境变量加载环境变量文件
  const ASR_ENV = dotenv.parse(fs.readFileSync(`.env.${mode}`))
  return {
    base: ASR_ENV.VITE_PUBLIC_PATH,
    plugins: [vue()],
    server: {
      host: '0.0.0.0',
      port: 3000,
      https: false,
      proxy: {
        [ASR_ENV.VITE_BASE_API]: {
          target: `${ASR_ENV.VITE_TARGET_HOST}`,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
    },
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          charset: false,
        }
      },
      postcss: {
        plugins: [
          postCssPxToRem({
            rootValue: 16, // 1rem的大小
            propList: ['*'], // 需转换的属性，这里选择全都进行转换
          })
        ]
      }
    },
    build: {
      minify: 'terser', // 是否进行压缩，boolean | terser | esbuild，默认使用esbuild
      manifest: false, // 是否产出maifest.json
      sourcemap: false, // 是否产出soucemap.json
      chunkSizeWarningLimit: 1500,
    },
  }
})
